package com.zx.platform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.zx.platform.dao.MenusDao;
import com.zx.platform.dao.model.Menus;
import com.zx.platform.dao.model.MenusExample;
import com.zx.platform.service.MenusService;
import com.zx.platform.service.domain.MenusDomain;
import com.zx.platform.service.model.MenusModel;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("menusService")
public class MenusServiceImpl implements MenusService {

	@Autowired
	private MenusDao dao;

	/**
	 * 得到所有数据列表
	 */
	public List getAllList() throws DataAccessException {

		List<Menus> list = dao.getListByExample(null);
		List<MenusModel> modelList = new ArrayList<MenusModel>();
		for (Menus o : list) {
			MenusModel temp = new MenusModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;

	}

	/**
	 * 根据单Key查找Model
	 */
	public MenusModel getModelByKey(Integer key) throws DataAccessException {
		Menus o = dao.selectByPrimaryKey(key);
		MenusModel model = new MenusModel();
		BeanUtils.copyProperties(o, model);
		return model;
	}

	/**
	 * 根据Model查找列表
	 */
	public List getListByModel(MenusModel model) throws DataAccessException {
		MenusExample example = new MenusExample();
		com.zx.platform.dao.model.MenusExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(model.getMenuName())) {
			criteria.andMenuNameLike('%'+model.getMenuName()+'%');
		}
		List<Menus> list = dao.getListByExample(example);
		List<MenusModel> modelList = new ArrayList<MenusModel>();
		for (Menus o : list) {
			MenusModel temp = new MenusModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}

	/**
	 * 新增
	 */
	public Integer add(MenusModel model) throws DataAccessException {
		Menus temp = new Menus();
		BeanUtils.copyProperties(model, temp);
		Integer key = dao.add(temp);
		return key;
	}

	/**
	 * 修改
	 */
	public Integer update(MenusModel model) throws DataAccessException {
		Menus temp = new Menus();
		BeanUtils.copyProperties(model, temp);
		dao.update(temp);
		return model.getMenuId();
	}

	/**
	 * 删除
	 */
	public void delete(MenusModel model) throws DataAccessException {
		Menus temp = new Menus();
		BeanUtils.copyProperties(model, temp);
		dao.delete(temp);
	}

	/**
	 * 分页查询
	 * 
	 * @param pageSize
	 * @param pageNum
	 * @param menuModel
	 * @return
	 * @throws DataAccessException
	 */
	public Map getPageByDomain(int pageSize, int pageNum, MenusDomain domain)
			throws DataAccessException {

		Map<String, Object> map = new HashMap<String, Object>();
		MenusExample example = new MenusExample();
		com.zx.platform.dao.model.MenusExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(domain.getMenuName())) {
			criteria.andMenuNameLike('%'+domain.getMenuName()+'%');
		}
		//排序add by wenjw 2015.6.2
		example.setOrderByClause("create_Date desc");
		int pageBegin = (pageNum - 1) * pageSize;
		List<Menus> list = dao.getPageByExample(pageSize, pageBegin, example);
		List<MenusModel> menuModelList = new ArrayList<MenusModel>();
		for (Menus o : list) {
			MenusModel temp = new MenusModel();
			BeanUtils.copyProperties(o, temp);
			menuModelList.add(temp);
		}
		int count = dao.countByExample(example);
		map.put("count", count);
		map.put("list", menuModelList);
		return map;
	}

	/**
	 * 根据roleId获得相关联的menuList
	 * @param model
	 * @return
	 * @throws DataAccessException
	 */
	public List getMenusByRoleId(Integer roleId) throws DataAccessException {
		return dao.getMenusByRoleId(roleId);
	}
}
